package com.zhaoyuxi.redpack.service.impl;

import com.zhaoyuxi.redpack.entity.TbRedpack;
import com.zhaoyuxi.redpack.entity.TbUserRedpack;
import com.zhaoyuxi.redpack.mapper.TbUserRedpackMapper;
import com.zhaoyuxi.redpack.service.ITbRedpackService;
import com.zhaoyuxi.redpack.service.ITbUserRedpackService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhaoyuxi.redpack.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-04-02
 */
@Service
@Slf4j
public class TbUserRedpackServiceImpl extends ServiceImpl<TbUserRedpackMapper, TbUserRedpack> implements ITbUserRedpackService {

    @Autowired
    private ITbRedpackService iTbRedpackService;

    @Autowired
    private RedisService redisService;

    @Override
    public String getPack() {
        try {
            TbRedpack tbRedpack = iTbRedpackService.getOne(1);
            if (tbRedpack.getRemain() > 0) {
                tbRedpack.setRemain(tbRedpack.getRemain() - 1);
                boolean updateById = iTbRedpackService.updateById(tbRedpack);
                if (updateById) {
                    //3.新增用户抢红包记录
                    TbUserRedpack userRedpack = new TbUserRedpack();
                    userRedpack.setUserId(tbRedpack.getUserId());
                    userRedpack.setRedpackId(tbRedpack.getId());
                    userRedpack.setGrabDate(new Date());
                    userRedpack.setAmount(new BigDecimal(String.valueOf(tbRedpack.getAmount())));
                    this.baseMapper.insert(userRedpack);
                    return "抢红包成功";
                }

            }
            //异步通知用户抢红包成功
        } catch (Exception e) {
            log.error("处理抢单异常：" + e.getMessage());
            throw new RuntimeException("处理抢单异常");
        } finally {
            return "抢红包成功";
        }
    }

    @Override
    public String getPackRedis() {
        try {
            while (true) {
                TbRedpack tbRedpack = redisService.selectRemain();
                if (tbRedpack.getRemain() > 0) {
                    boolean update = redisService.update(tbRedpack.getVersion());
                    if (update) {
                        //3.新增用户抢红包记录
                        redisService.addRedPackDetail();
                        return "抢红包成功";
                    }
                }
            }
            //异步通知用户抢红包成功
        } catch (Exception e) {
            log.error("处理抢单异常：" + e.getMessage());
            throw new RuntimeException("处理抢单异常");
        } finally {
            return "抢红包成功";
        }
    }

    @Override
    public String getPackRedis2() {
        try {
            while (true) {
                TbRedpack tbRedpack = redisService.selectRemain();
                if (tbRedpack.getRemain() > 0) {
                    boolean update = redisService.update2();
                    if (update) {
                        //3.新增用户抢红包记录
                        redisService.addRedPackDetail();
                        return "抢红包成功";
                    }
                }
            }
            //异步通知用户抢红包成功
        } catch (Exception e) {
            log.error("处理抢单异常：" + e.getMessage());
            throw new RuntimeException("处理抢单异常");
        } finally {
            return "抢红包成功";
        }
    }
}
